package com.example.socialwebserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.socialwebserver.mapper.UserMapper;
import com.example.socialwebserver.pojo.*;
import com.example.socialwebserver.service.LoginService;
import com.example.socialwebserver.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    LoginService loginService;
    @Override
    public List<UserDAO> SelectAll() {
       List<UserDAO> userDAOList = userMapper.selectList(null);
        return userDAOList;
    }
    @Override
    public UserVO SelectById(Integer id) {
        LoginDAO loginDAO = loginService.SelectById(id);
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("id", id);
        UserDAO userDAO = userMapper.selectOne(queryWrapper1);
        if(userDAO == null){
            return null;
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userDAO, userVO);
        userVO.setUserName(loginDAO.getUserName());
        return userVO;
    }
    @Override
    public boolean InsertUser(UserDAO userDAO) {
        return userMapper.insert(userDAO) == 1?true:false;
    }
    @Override
    public boolean UpdateById(UserDAO userDAO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", userDAO.getId());
        return userMapper.update(userDAO,queryWrapper) == 1?true:false;
    }
    @Override
    public boolean DeleteById(Integer id) {
        return userMapper.deleteById(id)== 1?true:false;
    }
}
